package com.codemart.mapper;

import com.codemart.dto.*;
import com.codemart.exceptions.ServiceException;
import com.codemart.vo.ProjectInfoVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProjectMapper {

    List<ProjectInfoVO> getProjectInfo();

    List<ProjectInfoVO> getProjectInfoByTag(String tag);

    ProjectDetailDTO getProjectDetail(Integer deid);

    @Select("""
    select
        plan_id,
        plan_name
    from
        plan_info
""")
    List<PlansInfoDTO> getPlans();

    @Select("""
    select
        *
    from
        sku_info
""")
    List<SkuInfoDTO> getSkus();


    @Insert("insert into demand_info (`title`) values (#{title})")
    @Options(useGeneratedKeys = true, keyProperty = "deId", keyColumn = "de_id")
    int insertDemandInfo(ProjectSaveInfoDTO psi);

    @Insert("insert into demand_contenxt (de_id, de_context) values (#{deId}, #{context})")
    void insertDemandContext(@Param("deId") Integer deId, @Param("context") String context);

    default void projectSave(ProjectSaveInfoDTO psi, ProjectMapper mapper) {
        int rowsAffected = mapper.insertDemandInfo(psi);
        Integer lastInsertedId = psi.getDeId();

        if (rowsAffected > 0 && lastInsertedId != null) {
           try{
               mapper.insertDemandContext(lastInsertedId, psi.getContext());
               if(!psi.getSku().isEmpty()){
                   psi.getSku().forEach((sku)->{
                       mapper.insertDemandSku(lastInsertedId,sku);
                   });
               }
               mapper.insertDemandPlan(lastInsertedId);
               mapper.insertDemandDetail(lastInsertedId,psi.getAuthorId(),psi.getMoney(),psi.getDays());
               mapper.insertDemandStatus(lastInsertedId);
           }catch (Exception e){
               e.printStackTrace();
               throw new ServiceException("提交错误,请联系客服或管理人员");
           }
        } else {
            throw new ServiceException("提交错误");
        }
    }

    @Insert("insert into demand_status (de_id,status_id) values (#{deId},1)")
    void insertDemandStatus(Integer deId);

    @Insert("insert into demand_detail (de_id,author_id,money,term_day) values (#{deId}, #{authorId}, #{money}, #{days})")
    void insertDemandDetail(Integer deId, Integer authorId, Double money, Integer days);

    @Insert("insert into demand_plan (de_id,plan_id) values (#{deId}, 1)")
    void insertDemandPlan(Integer deId);

    @Insert("insert into demand_sku (de_id,sku_id) values (#{deId}, #{sku})")
    void insertDemandSku(Integer deId, Integer sku);


    @Update("update demand_status set status_id = #{statusId} where de_id = #{de_id}")
    void updateProjectStatus(Integer de_id, Integer statusId);

    @Select("select dui_id from demand_user_info where de_id = #{deid}")
    List<Integer> getJoinPersonList(Integer deid);

    @Update("update demand_plan set plan_id = #{planId} where de_id = #{de_id}")
    void updateProjectPlan(Integer de_id, Integer planId);

    @Insert("insert into demand_user_info (u_id,de_id) values (#{user_id},#{deId});")
    @Options(useGeneratedKeys = true, keyProperty = "dui_id", keyColumn = "dui_id")
    void joinProject(JoinProjectInfoDTO jpi);

    ProjectListDTO showJoinProjectById(Integer userid, Integer deid);

    @Select("select author_id from demand_detail where de_id = #{deid}")
    Integer showDemandIsWho(Integer deid);

    @Update("update demand_info set person = (select count(1) from demand_user_info where de_id = #{deId}) where de_id = #{deId}")
    void updatePerson(Integer deId);

    List<ListPersonDTO> listJoinPerson(Integer de_id);

    @Insert("insert into demand_user_status (dui_id) values (#{dui_id})")
    void setDemandUserInfo(@Param("dui_id") Integer dui_id);

    @Update("update demand_user_status set status_id = #{statusId} where dui_id = #{dui}")
    void closeJoinProject(Integer dui,Integer statusId);

    @Insert("insert into demand_select_of_dibs (u_id,de_id) values (#{uid},#{deid})")
    void isBid(Integer uid, Integer deid);

    List<ProjectInfoVO> getProjectInfoByTitle(String title);
}
